package com.example.demo.leetcode;

public class _1599_经营摩天轮的最大利润 {

    public static void main(String[] args) {
        int[] customers = {8, 3};


        int[] customers1 = {4, 0, 0, 0, 0, 3};
        int[] customers2 = {5, 0, 0, 0, 0, 30};
        int i = minOperationsMaxProfit(customers2, 5, 5);
        System.out.println(i);
    }

    public static int minOperationsMaxProfit(int[] customers, int boardingCost, int runningCost) {
        if (runningCost > boardingCost * 4) {
            return -1;
        }
        int length = customers.length;
        int max = 0;
        int ans = 1;
        int wait = 0;
        int profit = 0;
        for (int i = 0; i < length; i++) {
            if (wait + customers[i] >= 4) {
                profit += 4 * boardingCost - runningCost;
                wait += customers[i] - 4;
            } else {
                profit += (wait + customers[i]) * boardingCost - runningCost;
                wait = 0;
            }
            if (profit > max) {
                max = profit;
                ans = i + 1;
            }
        }

        while (wait > 0) {
            if (wait >= 4) {
                profit += 4 * boardingCost - runningCost;
                wait -= 4;
            } else {
                profit += wait * boardingCost - runningCost;
                wait = 0;
            }
            if (profit > max) {
                max = profit;
                if (ans < length) {
                    ans = length;
                }
                ans++;
            }
        }

        if (max > 0) {
            return ans;
        }
        return -1;
    }
}
